package com.zzty.servlet;

import com.zzty.entity.Group;
import com.zzty.entity.User;
import com.zzty.service.GroupService;
import com.zzty.serviceimpl.GroupServiceImpl;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "GroupServlet",urlPatterns = "/GroupServlet")
public class GroupServlet extends HttpServlet {
    GroupService gs=new GroupServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Group group=new Group();
        group.setUserName1(request.getParameter("userName1"));
        group.setUserName2(request.getParameter("userName2"));
        boolean result=false;
        result=gs.insert(group);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if("getUsers".equals(method)){
            getUsers(request,response);
        }
        if("getGroupIdByName".equals(method)){
            getGroupIdByName(request,response);
        }
        if("checkGrouped".equals(method)){
            checkGrouped(request,response);
        }
        if("getGroups".equals(method)){
            getGroups(request,response);
        }
        if("deletes".equals(method)){
            deletes(request,response);
        }
        if("getGroupNames".equals(method)){
            getGroupNames(request,response);
        }
    }
    protected void getGroupNames(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user=(User)request.getSession().getAttribute("user");
        String name=user.getName();
        String userNames=gs.getUserNamesByUserName(name);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(userNames);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    protected void getUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<User> userNames=gs.getUsers();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(userNames);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void getGroupIdByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       User user=(User)request.getSession().getAttribute("user");
       String name=user.getName();
       Integer groupId=gs.getIdByUserName(name);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(groupId);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    protected void checkGrouped(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user=(User)request.getSession().getAttribute("user");
        String name=user.getName();
        Integer count=gs.checkGrouped(name);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(count);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void getGroups(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Group> groups=gs.getGroups();
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(groups);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void deletes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer groupId=Integer.parseInt(request.getParameter("groupId"));
        boolean result=gs.deletes(groupId);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

}
